package com.example.tasks;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * 示例热插拔任务3：系统监控任务
 */
public class SystemMonitorTask {
    
    private static final Random random = new Random();
    
    /**
     * CPU使用率检查
     */
    public String checkCpuUsage() {
        // 模拟获取CPU使用率
        double cpuUsage = 10 + (random.nextDouble() * 80); // 10-90%之间
        
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        String message = String.format("[%s] CPU使用率: %.2f%%", timestamp, cpuUsage);
        System.out.println("[SystemMonitorTask] " + message);
        
        // 如果CPU使用率过高，发出警告
        if (cpuUsage > 80) {
            String warning = "警告: CPU使用率过高! " + message;
            System.out.println("[SystemMonitorTask] " + warning);
            return warning;
        }
        
        return message;
    }
    
    /**
     * 内存使用率检查
     */
    public String checkMemoryUsage() {
        // 模拟获取内存使用情况
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();
        long usedMemory = totalMemory - freeMemory;
        double usagePercent = (double) usedMemory / totalMemory * 100;
        
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        String message = String.format("[%s] 内存使用率: %.2f%% (已用: %d MB, 总计: %d MB)", 
            timestamp, usagePercent, 
            usedMemory / (1024 * 1024), 
            totalMemory / (1024 * 1024));
        
        System.out.println("[SystemMonitorTask] " + message);
        return message;
    }
    
    /**
     * 磁盘空间检查
     */
    public String checkDiskSpace() {
        // 模拟磁盘空间检查
        double freeSpaceGB = 50 + (random.nextDouble() * 100); // 50-150GB
        double totalSpaceGB = 200;
        double usagePercent = (totalSpaceGB - freeSpaceGB) / totalSpaceGB * 100;
        
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        String message = String.format("[%s] 磁盘使用率: %.2f%% (可用: %.2f GB, 总计: %.2f GB)", 
            timestamp, usagePercent, freeSpaceGB, totalSpaceGB);
        
        System.out.println("[SystemMonitorTask] " + message);
        
        if (usagePercent > 90) {
            String warning = "警告: 磁盘空间不足! " + message;
            System.out.println("[SystemMonitorTask] " + warning);
            return warning;
        }
        
        return message;
    }
    
    /**
     * 综合健康检查
     */
    public String healthCheck() {
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
        // 执行各项检查
        String cpuStatus = checkCpuUsage();
        String memStatus = checkMemoryUsage();
        String diskStatus = checkDiskSpace();
        
        boolean hasWarning = cpuStatus.contains("警告") || memStatus.contains("警告") || diskStatus.contains("警告");
        
        String overallStatus = hasWarning ? "系统状态: 有警告" : "系统状态: 正常";
        String result = String.format("[%s] 系统健康检查完成 - %s", timestamp, overallStatus);
        
        System.out.println("[SystemMonitorTask] " + result);
        return result;
    }
}